﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class KnifeTypeEdit : AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblKnifeTypeID.Attributes.Add("readonly", "true");
                
                BindData();
            }
            Master.ValidationGroup = "gSubmit";
        }
      
        private void BindData()
        {
            string strCode = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            if (!string.IsNullOrEmpty(strCode))
            {
                MdKnifetypeEntity en = MdKnifetype.GetMdKnifetypeByID(strCode);
                if (en != null)
                {
                    this.lblKnifeTypeID.Text = en.KnifeTypeID;
                    this.txtKnifeTypeName.Text = en.KnifeTypeName;
                  
                }
            }
        }
      
    

      

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写刀具类型资料！");
                    return;
                }
                if (!string.IsNullOrEmpty(this.txtKnifeTypeName.Text.Trim()))
                {
                    if ((new MdKnifetype()).IsNameExists(txtKnifeTypeName.Text.Trim(),lblKnifeTypeID.Text))
                    {
                        UIHelper.AlertMessage(this.Page, "刀具类型已存在，请重新填写！");
                        return;
                    }
                    MdKnifetypeEntity Knife = new MdKnifetypeEntity();
                    Knife.KnifeTypeID = this.lblKnifeTypeID.Text.Trim();
                    Knife.KnifeTypeName = this.txtKnifeTypeName.Text.Trim();
                   
                    Knife.Status = "1";
                    Knife.ModifyDate = DateTime.Now;
                    Knife.ModifyUserID = CurrentUser.UserID;
                    Knife.ModifyUserName = CurrentUser.UserName;
                    
                    if (MdKnifetype.Update(Knife))
                    {
                        base.LogAction(BaseEnum.ActionType.Modify, Knife.KnifeTypeID, true, null, new EntityBase[] { Knife });
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/KnifeTypeList.aspx");
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    }
                }

            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }
    }
}